<?php
require_once ('BaseCateController.php');
class Article_cateController extends ManagerController {
	var $cate_name;
	var $cate_mode;
	public function Article_cateController(){
		$this->cate_name = '文章';
		$this->cate_mode = new Article_cateModel();
		parent::ManagerController();
	}
	
	public function index(){
		$this->page_title = $this->cate_name . '分类管理 - '. $this->site_info['name'];
		$cates = $this->get_cates(SITE_ID, 'name,parent,'.LANGUAGE.'.name');
		$tree = new Tree($cates);
		$this->assign('cates', $tree->get_formated_list());
		$this->display();
	}
	
	public function edit(){
		$h = new FormHelper(array('name'=>'article_cate', 'add_site_id'=>true));
		if(IS_POST){
// 			$result = $h->get_post_data();
// 			if($result['result']===false){
// 				$this->ajax_error($result);
// 			}
// 			$cate = $result['data'];
// 			$mutil_lan_names = array('name');
// 			global $site_info;
// 			$lans = array_keys($site_info['langs']);
// 			foreach ($lans as $lan){
// 				$name_len = utf8_strlen($cate[$lan]['name']);
// 				if($name_len<1 || $name_len>50){
// 					$this->ajax_error('分类名称['.Lang::get_lan_name($lan).']长度应该在1至50之间！');
// 				}
// 			}
// 			$result['data']['site_id'] = SITE_ID;
// 			if(!empty($this->id)){
// 				$result['data']['_id'] = $this->id;
// 			}
// 			$r = var_export($result, true);
// 			$this->ajax_error($r);
			$result = $h->save($result);
			$this->ajax_info($result);
		}
		$this->display();
	}

	/**
	 * @param unknown_type $site_id
	 * @param unknown_type $columns
	 * @param unknown_type $lang
	 * @return unknown
	 */
	protected function get_cates($site_id, $columns=null, $lang=null){
		if($lang==null) $lang = LANGUAGE;
		$cates = $this->cate_mode->find(array('site_id'=>$site_id), $columns);
		foreach($cates as $cate_id=>&$cate){
			if(isset($cate[$lang])){
				foreach($cate[$lang] as $k=>$v){
					$cate[$k] = $v;
				}
				unset($cate[$lang]);
			}
		}
		return $cates;
	}
	

	public function del(){
		if(!isset($this->id)){
			$this->ajax_error('未指定删除记录！');
		}
	
		$mod = new Article_cateModel();
		$cate = $mod->get_by_key($this->id, 'parent');
		if(empty($cate)){
			$this->ajax_error('要删除的记录不存在或者已经删除！');
		}
		//将原来下面的分类向上移
		$mod->update_by_attrs(array('parent'=>$this->id, 'site_id'=>SITE_ID),array('parent'=>$cate['parent']));
	
		$mod->delete_by_key($this->id);
		$this->ajax_info('删除成功！');
	}
}

?>